/*
 * Copyright (c) 2012 Android Dev Freelancer Team. All Rights Reserved.
 */
package com.devgroupvn.dota;

import java.io.IOException;

import android.os.AsyncTask;
import android.util.Log;

/* 
 * @class Async
 * @author tkminh
 * @version 1.0, 24 May 2012
 * 
 */


public class Async extends AsyncTask<Void, Void, Void> {

	public String url;
	public PostAdapter adapter;
	
	/**
	 * Init with post adapter and url
	 * @param _url
	 * @param _adapter : it should be from VideoDota2Activity
	 */
	public Async(String _url, PostAdapter _adapter) {
		url = _url;
		adapter = _adapter;
	}
	
	/**
	 * this function is used to run in background
	 */
	@Override
	protected Void doInBackground(Void... arg0) {
		loadPostList(url);
		return null;
	}
	
	/**
	 * this function is called when execute async
	 */
	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		
		VideoDota2Activity.page+=1;
		VideoDota2Activity.progressDialog.dismiss(); 
		//
		adapter.notifyDataSetChanged();
		//pb.setVisibility(View.GONE);
		//inflaterMore.setVisibility(View.VISIBLE);
	}
	
	/**
	 * function loadPostList
	 * @param url
	 * @des load the content to listView
	 */
	/*private void loadPostList(String url) {
		if (url == null) return;
		
		ViewPagerActivity.curcat = url;
		
		try {
			ViewPagerActivity.listpost = ViewPagerActivity.mWebContent.getPostList(url ); //+ "/page/" + VideoDota2Activity.index + "/"
			Log.d("mydebug3","troi oi");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		Entry _entry;
		String des;
		String link;
		String image;
		String date ;
		String title;
		ViewPagerActivity.mValText = new String[ViewPagerActivity.listpost.size()];
		int i = 0;
		for (Post post : ViewPagerActivity.listpost) {
			des = post.title;
			link = post.link;
			image = post.img;
			title = post.title;
			date = post.date;
			i=i+1;
			_entry = new Entry(des, image, link);
			_entry.title=title;
			_entry.date= date;
			ViewPagerActivity.listData.add(_entry);
			Log.d("mydebug3","~~~" + des);
		}
	}*/
	private void loadPostList(String url) {
		if (url == null) return;
		
		VideoDota2Activity.curcat = url;
		
		try {
			VideoDota2Activity.listpost = VideoDota2Activity.mWebContent.getPostList(url ); //+ "/page/" + VideoDota2Activity.index + "/"
			Log.d("mydebug3","troi oi");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		Entry _entry;
		String des;
		String link;
		String image;
		String date ;
		String title;
		VideoDota2Activity.mValText = new String[VideoDota2Activity.listpost.size()];
		int i = 0;
		for (Post post : VideoDota2Activity.listpost) {
			des = post.title;
			link = post.link;
			image = post.img;
			title = post.title;
			date = post.date;
			i=i+1;
			_entry = new Entry(des, image, link);
			_entry.title=title;
			_entry.date= date;
			VideoDota2Activity.listData.add(_entry);
			Log.d("mydebug3","~~~" + des);
		}
	}
}
